package 栈;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author: TTDB
 * @create: 2025/9/22
 */
public class dailyTemperatures {
    public int[] dailyTemperatures(int[] temperatures) {
        //建立一个栈，栈内存放索引，当栈内元素对应的温度小于当前温度时，栈内元素出栈
        int n = temperatures.length;
        Deque<Integer> stack = new LinkedList<>();
        int[] ans = new int[n];
        for(int i = 0; i < n; i++){
            while(!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]){
                int index = stack.pop();
                ans[index] = i - index;
            }
            stack.push(i);
        }
        while(!stack.isEmpty()){
            ans[stack.pop()] = 0;
        }
        return ans;
    }
}
